const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNo: 1,
    pageSize: 20,
    initTabList: [{
        label: '二次元',
        // value: '二次元',
      },
      {
        label: '动物',
        // value: '动物',
      },
      {
        label: '美女',
        // value: '美女',
      },
      {
        label: 'LOL',
        // value: 'LOL',
      },
      {
        label: '风景',
        // value: '风景',
      },
      {
        label: '建筑',
        // value: '建筑',
      },
      {
        label: '卡通',
        // value: '卡通',
      },
    ],
    tabList: [],
    word: '卡通',
    tabActive: 0,
    items: [],
    pdVisible: false,
    pdText: '',
    loadingVisible: true,
    modalVisible: false,
    modalInputVal: '',
    deleteVisible: false,
    deleteVal: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.clear()
    let list = wx.getStorageSync('newTabList') || []
    this.setData({
      tabList: this.data.initTabList.concat(list)
    })
    wx.pageScrollTo({
      scrollTop: 0
    })
    this.getData()
  },
  getData() {
    let word = this.data.tabList[this.data.tabActive].label
    let items = this.data.items;
    this.setData({
      loadingVisible: true
    })
    wx.request({
      url: app.$api.pictureInterface,
      data: {
        word: word,
        queryWord: word,
        pn: this.data.pageNo,
        rn: this.data.pageSize
      },
      success: res => {
        if (res.statusCode == 200 && res.data.data.length) {
          let data = res.data.data
          data.splice(data.length - 1)
          data.forEach(item => {
            item.thumbURL = app.$util.httpToHttps(item.thumbURL)
          })
          this.setData({
            items: items.concat(data),
            pageNo: this.data.pageNo + 1,
          })
        }
      },
      fail: res => {
        // 
      },
      complete: res => {
        wx.stopPullDownRefresh();
        this.setData({
          loadingVisible: false
        })
      }
    })
  },
  handleChange({
    detail
  }) {
    this.setData({
      tabActive: detail.key,
      pageNo: 1,
      items: []
    });
    wx.pageScrollTo({
      scrollTop: 0
    })
    setTimeout(_ => {
      this.getData()
    }, 200)
  },
  handleLongtap(e) {
    let item = e.currentTarget.dataset['item'];
    if (item.id !== undefined) {
      this.setData({
        deleteVal: item.label,
        deleteIndex: item.index,
        deleteVisible: true
      })
    }
  },
  // 新增tab，缓存在本地
  handleAddTab() {
    this.setData({
      modalVisible: true,
      modalInputVal: ''
    })
  },
  handleOk(e) {
    let val = this.data.modalInputVal
    if (val.trim() == '') return
    let list = wx.getStorageSync('newTabList') || []
    list.push({
      label: val,
      id: val
      // value: val
    })
    this.reSort(list)
    wx.setStorageSync('newTabList', list)
    app.$Toast({
      content: '新增成功',
      type: 'success'
    });
    let tabList = this.data.initTabList.concat(list)
    this.setData({
      tabList: tabList,
      modalVisible: false,
      items: [],
      tabActive: tabList.length - 1
    })
    this.getData()
  },
  handleCancel(e) {
    this.setData({
      modalVisible: false
    })
  },
  // 重新排序
  reSort(list) {
    list.forEach((item, index) => {
      item.index = index
    })
    return list
  },
  handleOk2() {
    let list = wx.getStorageSync('newTabList') || []
    list.splice(this.data.deleteIndex, 1)
    this.reSort(list)
    wx.setStorageSync('newTabList', list)
    let index = this.data.tabActive - 1
    if (index < 0) index = 0
    this.setData({
      tabList: this.data.initTabList.concat(list),
      deleteVisible: false,
      items: [],
      tabActive: index
    })
    this.getData()
  },
  handleCancel2(e) {
    this.setData({
      deleteVisible: false
    })
  },
  modalInputChange(e) {
    let val = e.detail.detail.value
    val = val.trim()
    this.setData({
      modalInputVal: val
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageNo: 1,
      items: []
    })
    this.getData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    this.getData()
  },
  openWebView(e) {
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: `/pages/webview/webview?weixin_url=${url}`
    });
  }

})